package com.itheima.reggie.common;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Map;

//JWT的生成和解析
public class JwtUtil {
    // 创建token
    public static String createToken(Map claims) {
        return Jwts.builder()
                .setClaims(claims) //设置响应数据体
                .signWith(SignatureAlgorithm.HS256, "reggie") //设置加密方法和加密盐
                .compact();
    }

    // 解析token
    public static Map parseToken(String token) {
        try {
            return Jwts.parser().setSigningKey("reggie")
                    .parseClaimsJws(token)
                    .getBody();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /*public static void main(String[] args) {
        // 制作token
       *//* Map map =  new HashMap();
        map.put("id", 10121231);
        map.put("phone", "13500135000");

        String token = createToken(map);
        System.out.println(token);*//*
s
        // 解析token
        Map map = parseToken("eyJhbGciOiJIUzI1NiJ9.eyJwaG9uZSI6IjEzNTAwMTM1MDAwIiwiaWQiOjEwMTIxMjMxfQ.ed2SkQgTVUTOheALPrnRVGfzym-QfALhgGAa0rw_BaI");
        System.out.println(map);
    }*/
}